import Vuex from 'vuex'
import defaultState from './state/state'
import mutations from './mutations/mutations'
import getters from './getters/getters'
import actions from './actions/actions'

export default () => {
  // return new Vuex.Store({
  // 热更替
  const store = new Vuex.Store({
    state: defaultState,
    mutations,
    getters,
    actions,
    modules: {
      a: {
        namespaced: true,
        state: {
          text: '你好a'
        },
        mutations: {
          updateText (state, txt) {
            state.text = txt
          }
        },
        getters: {
          textPlus (state, getters, rootState) {
            return state.text + '子getters' + rootState.b.text
          }
        },
        actions: {
          actionsA ({state, commit, rootState}) {
            // 调用全局
            commit('updateCount', {num: 233}, {root: true})
            // commit('updateText', rootState.count)
          }
        }
      },
      b: {
        namespaced: true,
        state: {
          text: '你好b'
        },
        actions: {
          actionsB ({commit}) {
            commit('a/updateText', 'actionsB', {root: true})
          }
        }
      }
    },
    plugins: [
      (store) => {
        console.log('plugins')
      }
    ]
  })
  if (module.hot) {
    module.hot.accept([
      './state/state',
      './mutations/mutations',
      './getters/getters',
      './actions/actions'
    ], () => {
      const newState = require('./state/state').dafault
      const newMutations = require('./mutations/mutations').dafault
      const newGetters = require('./getters/getters').dafault
      const newActions = require('./actions/actions').dafault
      store.hotUpdate({
        state: newState,
        getters: newGetters,
        mutations: newMutations,
        actions: newActions
      })
    })
  }
  return store
}
